package servlet.admin;

import com.alibaba.fastjson.JSON;
import pojo.Result;
import pojo.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet("/admin/searchUsersServlet")
public class SearchUsersServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取请求参数
        String nickName = req.getParameter("loginName");
        String status = req.getParameter("userStatus");
        // 设置字符编码
        resp.setContentType("application/json;charset=UTF-8");
        List<User> users = userService.selectUserListByNameAndStatus(nickName, status);
        Result result = null;
        if (users != null) {
            result = Result.success(users);
        } else {
            result = Result.error("查询失败");
        }
        resp.getWriter().write(JSON.toJSONString(result));
    }
}
